﻿define(['app', 'services/friendlyUrlService'], function (app) {
    app.lazy.filter("getCategoryById", function () {
        return function (key, categories) {
            if (key && categories && categories[key]) return categories[key];
            return "";
        }
    });
    app.lazy.controller("EditBookController",
        [
            "$scope",
            "$rootScope",
            "$routeParams",
            "$resource",
            "$timeout",
            "$http",
            "$browser",
            "friendlyUrlService",
            "notifyService",
            function ($scope, $rootScope, $routeParams, $resource, $timeout, $http, $browser, friendlyUrlService, notifyService) {
                var bookId = $routeParams.id;

                var bookService = $resource("/dashboard/services/book/:id", { id: '@id' });
            
                var queryBooks = function () {
                    var book;
                    if (!bookId) {
                        book = new bookService({});
                        $scope.book = book;
                        return;
                    }

                    book = bookService.get({ id: bookId }, function () {
                        $scope.book = book;
                        $rootScope.title = book.Name;
                    
                        book.selectedCategories = [];
                        angular.forEach(book.Categories, function (categoryId) {
                            var category = $scope.categories[categoryId];
                            if (category) {
                                book.selectedCategories.push(category);
                                category.selected = true;
                            }
                        });
                    });
                };

                $scope.nameKeyUp = function () {
                    var book = $scope.book;
                    if (book) book.NameOnUrl = friendlyUrlService.toUrlFriendly(book.Name);
                };

                $scope.tinymceOptions = {
                    handle_event_callback: function (e) {
                        // put logic here for keypress
                    }
                };
                
                // category
                $http.get("/dashboard/services/category").success(function (data) {
                    $scope.categories = data;

                    queryBooks();
                });
                
                $scope.categoryPanel = {
                    display: false,
                    toggle: function () {
                        this.display = !this.display;
                    }
                };
                                
                $scope.selectCategory = function (category) {
                    if (!category.selected) {
                        category.selected = true;
                        $scope.book.selectedCategories.push(category);
                    }
                };

                $scope.deselectCategory = function (category) {
                    if (category.selected) {
                        category.selected = false;
                        var index = $scope.book.selectedCategories.indexOf(category);

                        if (index >= 0) {
                            $scope.book.selectedCategories.splice(index, 1);
                        }
                    }
                };

                // tag
                $http.get("/dashboard/services/tags/getnames").success(function (data) {
                    $scope.tags = data;
                });

                $scope.addTag = function () {
                    if ($scope.newTag && $scope.newTag.length > 0) {
                        $scope.book.Tags.push($scope.newTag);
                        $scope.newTag = "";
                    }
                };

                $scope.removeTagIndex = function (index) {
                    if ($scope.book.Tags && $scope.book.Tags.length > index) {
                        $scope.book.Tags.splice(index, 1);
                    }
                };

                var defaultDate = new Date();
                var minDate = new Date(defaultDate.getFullYear(), defaultDate.getMonth(), defaultDate.getDate());
                defaultDate = new Date(defaultDate.getFullYear(), defaultDate.getMonth(), defaultDate.getDate() + 1);
                defaultDate.setHours(0);
                defaultDate.setMinutes(0);
                // datepicker
                var publish = {
                    type: $browser.cookies("publishType") ? $browser.cookies("publishType") : "SaveDraft",
                    date: defaultDate,
                    options: {
                        'year-format': "'yy'",
                        'starting-day': 1
                    },
                    open: function () {
                        publish.opened = !publish.opened;
                    },
                    opened: false,
                    min: minDate
                };
                                
                $scope.publish = publish;
                $scope.afterSavingAction = $browser.cookies("afterSavingAction") ? $browser.cookies("afterSavingAction") : "stay";
                
                $scope.save = function () {
                    $browser.cookies("publishType", $scope.publishType);
                    $browser.cookies("afterSavingAction", $scope.afterSavingAction);

                    $scope.book.Categories = [];
                    angular.forEach($scope.book.selectedCategories, function (category) {
                        $scope.book.Categories.push(category.Id);
                    });

                    $scope.book.Publishing = {
                        type: $scope.publish.type,
                        date: $scope.publish.date
                    };
                    
                    $scope.book.$save(function (book, responseHeaders) {
                        book.selectedCategories = [];
                        angular.forEach(book.Categories, function (categoryId) {
                            var category = $scope.categories[categoryId];
                            if (category) {
                                book.selectedCategories.push(category);
                                category.selected = true;
                            }
                        });
                    }, function (httpResponse) {
                        //alert(httpResponse.status);
                        //printArguments(httpResponse.data);
                    });
                };
            }
        ]);
});